package edu.ccut.saturn.studio.validate.part;

import java.util.List;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import edu.ccut.saturn.studio.saturndata.model.ValidateModel;

//import edu.ccut.saturn.studio.validate.model.ValidateModel;




/**
 * 表格单元修改器
 * @author Bai Yucheng
 * 
 */
public class TableViewerCellModifier implements ICellModifier {
	private TableViewer tv;
	private TableLabelProvider tableLabelProvider;
	private ValidateModel dm;
	private List<String> args;

	public TableViewerCellModifier(TableViewer tv,TableLabelProvider tableLabelProvider,ValidateModel dm,List<String> args) {
		this.tv = tv;
		this.tableLabelProvider = tableLabelProvider;
		this.dm = dm;
		this.args = args;
	}

	/**
	 * 返回是否可以被修改，true为可修改
	 */
	public boolean canModify(Object element, String property) {
		return true;
	}

	/**
	 *  返回列的值：参数element是表格记录的对象，property是表格列的属性
	 * 
	 */
	public Object getValue(Object element, String property) {
		String args = (String) element;
		if (property.equals(ValidateModel.ARGS_ELEMENT_ARG)) {
			return String.valueOf(args);
		} else {
			return null;
		}
	}

	/**
	 * 修改表格单元中的值
	 * 
	 * @param element
	 *            表格记录的对象
	 * @param property
	 *            表格列的属性
	 * @param value
	 *            修改的新值
	 */
	public void modify(Object element, String property, Object value) {
		TableItem tableItem = (TableItem) element;
		String oldValue = (String) tableItem.getData();
		
		if (property.equals(ValidateModel.ARGS_ELEMENT_ARG)) {
			String newValue = (String) value;
			if (newValue.equals("")) {
				return;
			} else {
				int i = 0;
				for (String arg : args) {
					if (arg.equals(oldValue)) {
						args.set(i, newValue);
						break;
					}
					++i;
				}
			}
		}
		
		dm.updateArg(args);
		tableLabelProvider.setRowid(0);
		tv.refresh();
	}
}